package com.fanly.weixin.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 获取数据工具类
 * @author fanlizhi
 *
 */
public class UrlUtils {

	public static String getData(String httpUrl,String httpArg){
		BufferedReader reader = null;
		String result = "";
		StringBuffer buffer = new StringBuffer();
		httpUrl = httpUrl+"?"+httpArg;
		try{
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
//			connection.setRequestProperty("apikey", getApiKey());
			connection.connect();
//			System.out.println("请求地址："+connection.getURL()+"\n"+connection.toString());
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
			String strRead = null;
			while((strRead=reader.readLine())!=null){
//				System.out.println("---"+strRead);
				buffer.append(strRead);
				buffer.append("\r\n");
			}
			reader.close();
			//返回的数据为json
			result = buffer.toString();
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return "";
		}
	}
	public static void main(String[] args) {
		String str = "12345";
		String str1 = "1111213";
		System.out.println(Integer.parseInt(str));
		System.out.println(str1.matches("[0-9]{6}"));
		String httpUrl = "http://apis.haoservice.com/lifeservice/postcode/query";
		String httpArg = "postcode=233300&page=1&pagesize=20&key=769ac334487d4d9182b917baa41e4299";   // http://apis.haoservice.com/lifeservice/postcode/query?postcode=215001&page=1&pagesize=20&key=您申请的APPKEY
		System.out.println(UrlUtils.getData(httpUrl, httpArg));		
	}
}
